using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace Widgetsphere.VsCodeTools.Forms
{
  public partial class StatisticsForm : Form
  {
    public StatisticsForm()
    {
      InitializeComponent();
      this.IsExpanded = false;
      lblHeader.Text = "These files were found in folders with generated files, however they were not part of the generation. They are most likely old files that need to be deleted. Please verify that you wish to remove these files.";
    }

    #region Class Members

    private bool _isExpanded = false;
    private ArrayList _generatedFileList = new ArrayList();

    #endregion

    #region Property Implementations

    public bool IsExpanded
    {
      get { return _isExpanded; }
      set
      {
        _isExpanded = value;

        if (this.IsExpanded)
        {
          //Expand
          this.FormBorderStyle = FormBorderStyle.Sizable;
          this.Size = new Size(this.MinimumSize.Width, this.MinimumSize.Height * 2);
          this.cmdDetails.Text = "<< Details";
        }
        else
        {
          //Shrink
          this.FormBorderStyle = FormBorderStyle.FixedDialog;
          this.Size = this.MinimumSize;
          this.cmdDetails.Text = "Details >>";
        }

      }
    }

    public string DisplayText
    {
      get { return txtStats.Text; }
      set { txtStats.Text = value; }
    }

    public ArrayList GeneratedFileList
    {
      get { return _generatedFileList; }
      set
      {
        try
        {
          _generatedFileList = value;
          if (_generatedFileList == null)
            _generatedFileList = new ArrayList();
          lstFile.Items.Clear();

          //Load the parent folders
          ArrayList folderList = new ArrayList();
          foreach (string fileName in this.GeneratedFileList)
          {
            if (fileName != "")
            {
              FileInfo fi = new FileInfo(fileName);
              if (!folderList.Contains(fi.DirectoryName))
                folderList.Add(fi.DirectoryName);
            }
          }

          //Now we have a folder list so load all files in all folders
          foreach (string folderName in folderList)
          {
            DirectoryInfo di = new DirectoryInfo(folderName);
            FileInfo[] fileList = di.GetFiles("*.*");
            foreach (FileInfo fi in fileList)
            {
              //Skip the projects
              if ((fi.Extension.ToLower() != ".csproj") &&
                (fi.Extension.ToLower() != ".scc") &&
                (fi.Extension.ToLower() != ".vssscc") &&
                (!fi.FullName.ToLower().Contains(".csproj.")))
              {
                lstFile.Items.Add(fi.FullName);
              }
            }
          }

          //Now loop and remove the items that were generated
          foreach (string fileName in this.GeneratedFileList)
          {
            if (fileName != "")
            {
              FileInfo fi = new FileInfo(fileName);
              lstFile.Items.Remove(fi.FullName);
            }
          }

        }
        catch (Exception ex)
        {
          throw;
        }
      }
    }

    #endregion

    #region Button Handlers

    private void cmdDetails_Click(object sender, EventArgs e)
    {      
      this.IsExpanded = !this.IsExpanded;      
    }

    private void cmdOK_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void cmdDelete_Click(object sender, EventArgs e)
    {
      if (MessageBox.Show("Do you wish to delete all checked files?", "Delete?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
      {
        try
        {
          ArrayList indexes = new ArrayList();
          for (int ii = 0; ii < lstFile.Items.Count; ii++)
          {
            if (this.lstFile.GetItemChecked(ii))
            {
              string fileName = (string)lstFile.Items[ii];
              indexes.Add(ii);
              FileInfo fi = new FileInfo(fileName);
              fi.Attributes = FileAttributes.Normal;
              fi.Delete();
            }
          }

          //Remove the deleted file entry
          for (int jj = indexes.Count - 1; jj >= 0;jj-- )
            lstFile.Items.RemoveAt((int)indexes[jj]);

        }
        catch (Exception ex)
        {
          throw;
        }
      }
    }

    private void cmdCheckAll_Click(object sender, EventArgs e)
    {
      for (int ii = 0; ii < lstFile.Items.Count; ii++)
        lstFile.SetItemChecked(ii, true);
    }

    private void cmdUncheckAll_Click(object sender, EventArgs e)
    {
      for (int ii = 0; ii < lstFile.Items.Count; ii++)
        lstFile.SetItemChecked(ii, false);
    }

    #endregion

  }
}